package cn.jinjimi.app.main.fragment;

import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import cn.jinjimi.base.util.StringUtils;
import cn.jinjimi.base.util.ToastUtils;
import cn.jinjimi.framework.helper.annotation.EventTrace;
import cn.jinjimi.framework.helper.annotation.EventTraceParam;
import cn.jinjimi.app.AccountManager;
import cn.jinjimi.app.R;
import cn.jinjimi.app.bean.EventDataField;
import cn.jinjimi.app.bean.EventID;
import cn.jinjimi.app.controller.UserController;
import cn.jinjimi.app.controller.event.DataEvent;
import cn.jinjimi.app.controller.event.LoginSuccessEvent;
import cn.jinjimi.app.controller.event.PerfectDetailEvent;
import cn.jinjimi.app.main.widget.MTextWatcher;
import cn.jinjimi.domain.data.dto.ProfileDTO;
import cn.jinjimi.app.main.activity.LoginActivity;
import cn.jinjimi.app.main.extend.BaseFragment;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * 设置微名片真实姓名
 * <p>
 * Created by HANER on 15/4/13.
 */
public class PerfectDetailFragment extends BaseFragment implements View.OnClickListener {

    private EditText realName;
    private Button cleanName;
    private TextView submit;

    @Override
    protected int getLayoutResourceId() {
        return R.layout.fragment_perfect_detail;
    }

    @Override
    protected void initial() {
        initView();
        initListener();
        getActivity().setTitle(R.string.perfect_detail);
    }

    protected void initView() {
        realName = (EditText) findView().findViewById(R.id.et_real_name);
        cleanName = (Button) findView().findViewById(R.id.bt_clean_real_name);
        submit =  findView().findViewById(R.id.bt_submit);
    }

    protected void initListener() {
        cleanName.setOnClickListener(this);
        submit.setOnClickListener(this);

        realName.addTextChangedListener(new MTextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (TextUtils.isEmpty(s)) {
                    cleanName.setVisibility(View.INVISIBLE);
                } else {
                    cleanName.setVisibility(View.VISIBLE);
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_submit:
                if (StringUtils.isChineseName(getActivity(), realName)) {
                    submit();
                }
                break;
            case R.id.bt_clean_real_name:
                realName.setText("");
                break;
        }
    }

    /**
     * 没有名字的用户登录请求
     */
    private void submit() {
        UserController.perfectDetail(realName.getText().toString().trim());
        ((LoginActivity) getActivity()).showLoadingIndicator();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onPerfectDetailEvent(PerfectDetailEvent event) {
        ((LoginActivity) getActivity()).hideLoadingIndicator();
        switch (event.state) {
            case DataEvent.SUCC:
                analyseLog(EventDataField.TypeSuccess, LoginActivity.getLoginFrom());
                ProfileDTO agent = event.data;
                AccountManager.getInstance().setLocalProfilesForLoadProfiles(agent.getProfile());
                //通知MainActivity登录成功
                EventBus.getDefault().post(new LoginSuccessEvent());
                break;
            case DataEvent.FAIL:
                analyseLog(EventDataField.TypeFail, LoginActivity.getLoginFrom());
                ToastUtils.toastMessage(getActivity(), "资料更新失败,请稍后再试");
                break;
        }
    }

    @EventTrace(EventID.APP_DATA_COMPLETE)
    private void analyseLog(@EventTraceParam(EventDataField.ParamType) String type, @EventTraceParam(EventDataField.ParamFrom) String from) {
    }
}
